﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRManagerUI.Profile
{
    public partial class MessageUI : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadMessages();
            }
        }

        private void LoadMessages()
        {
            MessageManager manager = new MessageManager();
            List<Message> messages = new List<Message>();
            Code senderCode = new Code();
            senderCode.CodeName = Session["Code"].ToString();
            messages = manager.ReatriveMessages(senderCode);
            messageGdv.DataSource = messages;
            messageGdv.DataBind();
            unreadLbl.Text = manager.CountUnRead(senderCode).ToString();
        }

        protected void messageGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            messageGdv.PageIndex = e.NewPageIndex;
            LoadMessages();
        }
        protected void DeleteMessage(object o, EventArgs e)
        {
            foreach (GridViewRow gvr in messageGdv.Rows)
            {
                CheckBox chkBox = (CheckBox)gvr.FindControl("check");
                if (chkBox != null && chkBox.Checked)
                {
                    int id = Convert.ToInt32(((Label)gvr.FindControl("lblId")).Text);
                    MessageManager manager = new MessageManager();
                    if (manager.DeleteMessage(id))
                    {
                        msgLbl.ForeColor = Color.Green;
                        msgLbl.Text = "Message Deleted Successfully";
                        LoadMessages();
                    }
                }
            }
        }
    }
}